package operations;

import book.Book;
import book.BookLiet;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookLiet bookLiet) {
        //删除图书
        System.out.println("删除图书！");
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你要添加的书籍名：");
        String bookName = input.nextLine();//输入要删除的书名

        //1.遍历数组 - 看看书否有要删除的书 - 有的话就记录下来
        int index = -1;
        int currentSize = bookLiet.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookLiet.getPos(i);
            if (bookName.equals(book.getName())) {
                index = i;
                break;
            }
        }
        if (-1 == index) {
            System.out.println("没有要删除的书！");
            return;
        }

        for (int i = index; i < currentSize - 1; i++) {
            Book book = bookLiet.getPos(i + 1);
            bookLiet.setBooks(book, i);
        }

        //每次删除都要置空
        bookLiet.setBooks(null, currentSize - 1);
        bookLiet.setUsedSize(currentSize - 1);
        System.out.println("删除成功！");
    }
}
